<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Chenyang_make_photo_gift_code extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Chenyangmakephotocustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_chenyang_make_photo_gift;
        $data['game_gift'] = array4js($g_chenyang_make_photo_gift);

        $this->load->view('admin/chenyang_make_photo_gift_code',$data);
    }

    public function getGiftCodeList() {
        $assist['where'] = getFilter($this->input->post(),Chenyangmakephotocustomermodel::$giftSearchField);
        $assist['like'] = getFilter($this->input->post(),Chenyangmakephotocustomermodel::$giftLikeField);
        $assist['sort'] = getSort($this->input->post(),Chenyangmakephotocustomermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        $assist['select'] = '
            chenyang_make_photo_customer.customer_id,
            chenyang_make_photo_customer.nickname,
            chenyang_make_photo_gift_code.*
        ';
        list($list,$count) = $this->m_customer->getGiftCodeListSection($assist);

        foreach ($list as $key => $value) {
            $list[$key]['nickname'] = urldecode($value['nickname']);
        }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createGiftCodeBatch() {
    	$this->validate->setRule('count','integer');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $list = array();
        for($i=0; $i < $info['count'];$i++) {
        	$list[] = array(
        		'gift_code' => md5($i.'-'.date('YmdHis').SALT),
        		'gift_code_time_create' => NOW,
        	);
        }
        $this->m_customer->createGiftCodeBatch($list);

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }

    public function exportGiftCodeList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '2048M');


        $assist['where'] = getFilter($this->input->get(),Chenyangmakephotocustomermodel::$giftSearchField);
        $assist['like'] = getFilter($this->input->get(),Chenyangmakephotocustomermodel::$giftLikeField);
        $assist['sort'] = getSort($this->input->get(),Chenyangmakephotocustomermodel::$sortField);

        $assist['select'] = '
            chenyang_make_photo_customer.customer_id,
            chenyang_make_photo_customer.nickname,
            chenyang_make_photo_gift_code.*
        ';
        list($list,$count) = $this->m_customer->getGiftCodeListSection($assist);

        foreach ($list as $key => $value) {
            $list[$key]['nickname'] = urldecode($value['nickname']);
        }


        $this->load->library('excel');
        $this->excel->init('晨阳水漆优惠码_'.date('YmdH').'.xls');
        $this->excel->addSheet('晨阳水漆优惠码');
        $this->excel->addTitle(array(
            array('content'=>'优惠码'),
            array('content'=>'顾客ID'),
            array('content'=>'顾客昵称'),
            array('content'=>'状态'),
            array('content'=>'中奖时间'),
            array('content'=>'创建时间'),
        ));
        

        global $g_chenyang_make_photo_gift;

        $sex_array = array(
            0 => '未知',
            1 => '男',
            2 => '女',
        );
        foreach ($list as $key => $value) {
            $value['nickname'] = urldecode($value['nickname']);
            $gift_info = array();

            $this->excel->addRow(array(
                array('content' => $value['gift_code']),
                array('content' => $value['customer_id']),
                array('content' => $value['nickname']),
                array('content' => $value['gift_code_status'] == STATUS_DISABLE ? '未中奖' : '已中奖'),
                array('content' => $value['gift_code_time_use']),
                array('content' => $value['gift_code_time_create']),
            ));
        }

        $this->excel->freezePanes(1,0);
        $this->excel->generate();
    }
}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */